//
//  UIViewController+ClickOnce.m
//  BadBoy
//
//  Created by wen on 2017/5/8.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "UIButton+ClickOnce.h"
#import <objc/runtime.h>
#define defaultInterval 0.2  //默认时间间隔

@interface UIButton ()

@property (assign) BOOL canCorrespond;

@end

@implementation UIButton (ClickOnce)

static const char *CanCorrespond = "CanCorrespond";

//- (void)setCanCorrespond:(BOOL)canCorrespond{
//    objc_setAssociatedObject(self, CanCorrespond, @(canCorrespond), OBJC_ASSOCIATION_ASSIGN);
////     objc_setAssociatedObject(self, CanCorrespond, canCorrespond, OBJC_ASSOCIATION_ASSIGN);
//}
//
//- (BOOL)canCorrespond{
//    return objc_getAssociatedObject(self, CanCorrespond);
//}

//+ (void)load
//{
//    // Method Swizzling
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        SEL selA = @selector(sendAction:to:forEvent:);
//        SEL selB = @selector(_wxd_sendAction:to:forEvent:);
//        Method methodA = class_getInstanceMethod(self,selA);
//        Method methodB = class_getInstanceMethod(self, selB);
//        
//        BOOL isAdd = class_addMethod(self, selA, method_getImplementation(methodB), method_getTypeEncoding(methodB));
//        
//        if (isAdd) {
//            class_replaceMethod(self, selB, method_getImplementation(methodA), method_getTypeEncoding(methodA));
//        }else{
//            //添加失败了 说明本类中有methodB的实现，此时只需要将methodA和methodB的IMP互换一下即可。
//            method_exchangeImplementations(methodA, methodB);
//        }
//    });
//}

- (void)_wxd_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    if (self.canCorrespond == NO) {
        [self _wxd_sendAction:action to:target forEvent:event];
        self.canCorrespond = YES;
        [self performSelector:@selector(restore) withObject:nil afterDelay:defaultInterval];
    }
    
}

- (void)restore{
    self.canCorrespond = NO;
}

@end
